home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Think Class Libraries / COrderedList 2.0 / CUnorderedList.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-30  |  7.5 KB  |  278 lines  |  [TEXT/KAHL]

  1. /******************************************************************************
  2.  CUnorderedList.c
  3.  
  4.     CUnorderedList maintains an unordered cluster of objects. It also adds 
  5.     methods for iterating over the list.
  6.     
  7.     Copyright (C) 1992 by Brown University. All rights reserved.
  8.  
  9.     Permission is granted to any individual or institution to use, copy,
  10.     or redistribute the binary version of this software and its
  11.     documentation provided this notice and the copyright notices are
  12.     retained.  Permission is granted to any individual or non-profit
  13.     institution to use, copy, modify, or redistribute the source files
  14.     of this software provided this notice and the copyright notices are
  15.     retained.  This software may not be distributed for profit, either
  16.     in original form or in derivative works, nor can the source be
  17.     distributed to other than an individual or a non-profit institution.
  18.     Any  individual or group interested in seeing and/or using these
  19.     source files but who are prevented from doing so by the above
  20.     constraints should contact Don Wolfe, Vice-President for Computer 
  21.     Systems at Brown University, (401) 863-7247, for possible
  22.     software licensing of the source developed at Brown.
  23.  
  24.     Brown University and Andrew James Gilmartin make no representations
  25.     about the suitability of this software for any purpose.
  26.  
  27.      BROWN UNIVERSITY AND ANDREW JAMES GILMARTIN GIVE NO WARRANTY, EITHER
  28.     EXPRESS OR IMPLIED, FOR THE PROGRAM AND/OR DOCUMENTATION PROVIDED,
  29.     INCLUDING, WITHOUT LIMITATION, WARRANTY OF MERCHANTABILITY AND
  30.     WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE.
  31.  
  32.     AUTHOR: Andrew_Gilmartin@Brown.Edu
  33.     MODIFIED: 93-04-12
  34.     
  35. ******************************************************************************/
  36.  
  37. #include "CUnorderedList.h"
  38.  
  39.  
  40. /*=============================================================================
  41.  DefaultCompare
  42.  
  43.     This is the default ordering function for the list. It orders the content
  44.     based on memory address (not too useful and ordering).
  45. =============================================================================*/
  46.  
  47. static int DefaultCompare( void* a, void* b )
  48. {
  49.     return (long) a - (long) b;
  50.  
  51. } /* DefaultCompare */
  52.  
  53.  
  54.  
  55. /******************************************************************************
  56.  IUnorderedList
  57.  
  58.     Initialize the list. Note that if no compare function is specified then
  59.     use the defualt.
  60. ******************************************************************************/
  61.  
  62. void CUnorderedList::IUnorderedList( CompareFunc compare )
  63. {
  64.     ICluster();
  65.  
  66.     SetCompare( compare );
  67.  
  68. } /* IUnorderedList */
  69.  
  70.  
  71.  
  72. /******************************************************************************
  73.  SetCompare
  74.  
  75.     Change the function used to order and compare items in the list. If the
  76.     list should be reordered immediatly, then pass TRUE to reorder. Note, if
  77.     the new compare function does not give the same ordering as the previous
  78.     FindIndex() will not work as expected.
  79. ******************************************************************************/
  80.  
  81. void CUnorderedList::SetCompare( CompareFunc compare  ) 
  82. {
  83.     fCompare = compare != NULL ? compare : (CompareFunc) DefaultCompare;
  84.  
  85. } /* SetCompare */
  86.  
  87.  
  88.  
  89. /******************************************************************************
  90.  FindIndex
  91.  
  92.     Find the offset of the object in the items list. This method uses bisection
  93.     to narrow down where the object is in the list. If the object is found then
  94.     return TRUE, otherwise return FALSE.
  95. ******************************************************************************/
  96.  
  97. Boolean CUnorderedList::FindIndex( CObject* anObject, long* foundindex ) 
  98. {
  99.     long index;
  100.     
  101.     for ( index = 0; index < numItems; index++ )
  102.     {
  103.         long relation = (*fCompare)( anObject, (CObject*) (*items)[ index ] );
  104.         
  105.         if ( relation == 0 )
  106.         {
  107.             *foundindex = index + 1;
  108.             return TRUE;
  109.         }
  110.     }
  111.  
  112.     *foundindex = numItems + 1;
  113.     return FALSE;
  114.  
  115. } /* FindIndex */
  116.  
  117.  
  118.  
  119. /******************************************************************************
  120.  AddAll
  121.  
  122.     Add all the objects in a cluster to the list.
  123. ******************************************************************************/
  124.  
  125.     static void _AddAll( CObject* anObject, CUnorderedList* aList )
  126.     {
  127.         aList->Add( anObject );
  128.     }
  129.  
  130. void CUnorderedList::AddAll( CCluster* aCluster )
  131. {
  132.     aCluster->DoForEach1( _AddAll, (long) this );
  133.  
  134. } /* AddAll */
  135.  
  136.  
  137.  
  138. /******************************************************************************
  139.  RemoveAll
  140.  
  141.     Remove all the objects from the list.
  142. ******************************************************************************/
  143.  
  144. void CUnorderedList::RemoveAll( void )
  145. {
  146.     while ( numItems > 0 )
  147.         DeleteItem( numItems );
  148.  
  149. } /* RemoveAll */
  150.  
  151.  
  152.  
  153. /******************************************************************************
  154.  Includes
  155.  
  156.     Does the list contain the object? Return TRUE if found, FALSE otherwise.
  157.     Modifies fCurrent to point to the found item; use Current() to retrieve
  158.     the object.
  159. ******************************************************************************/
  160.  
  161. Boolean    CUnorderedList::Includes( CObject* anObject )
  162. {
  163.     long index;
  164.     Boolean found;
  165.     
  166.     found = FindIndex( anObject, &index );
  167.     if ( found )
  168.         fCurrent = index;
  169.         
  170.     return found;
  171.  
  172. } /* Includes */
  173.  
  174.  
  175.  
  176. /******************************************************************************
  177.  At
  178.  
  179.     Return the object at a particular index. Note that it is not an error to
  180.     specify an index outside of the bounds of the list.
  181. ******************************************************************************/
  182.  
  183. CObject* CUnorderedList::At( long theIndex )
  184. {
  185.     if ( 1 <= theIndex && theIndex <= numItems )
  186.         return (CObject*) (*items)[ theIndex - 1 ];
  187.  
  188.     return NULL;
  189.      
  190. } /* At */
  191.  
  192.  
  193.  
  194. /******************************************************************************
  195.  First
  196.  
  197.     The methods First, Current, and Next are used to iterate over the content
  198.     of the list. The general form of the code is
  199.     
  200.         for ( list->First(); object = list->Current(); list->Next() )
  201.             ; // Do something
  202.              
  203.     First, Current, and Next use a cursor into the list. First sets the cursor
  204.     to point to the first object in the list; Current is used to get the object 
  205.     pointed at by the cursor; Next moves the cursor to the next object in the 
  206.     list.
  207. ******************************************************************************/
  208.  
  209. void CUnorderedList::First( void )
  210. {
  211.     fCurrent = 1;
  212.     
  213. } /* First */
  214.  
  215.  
  216.  
  217. /******************************************************************************
  218.  Next
  219.  
  220.     Move cursor to the next object in the list.
  221. ******************************************************************************/
  222.  
  223. void CUnorderedList::Next( void )
  224. {
  225.     fCurrent++;
  226.     
  227. } /* Next */
  228.  
  229.  
  230.  
  231. /******************************************************************************
  232.  Current
  233.  
  234.     Return the object at the cursor in the list.
  235. ******************************************************************************/
  236.  
  237. CObject* CUnorderedList::Current( void )
  238. {
  239.     return At( fCurrent );
  240.     
  241. } /* Current */
  242.  
  243.  
  244.  
  245. /******************************************************************************
  246.  CurrentIndex
  247.  
  248.     Return the index of the current object.
  249. ******************************************************************************/
  250.  
  251. long CUnorderedList::CurrentIndex( void )
  252. {
  253.     return fCurrent;
  254.     
  255. } /* CurrentIndex */
  256.  
  257.  
  258.  
  259. /******************************************************************************
  260.  __ForgetCollection
  261.  
  262.     Dispose of the collection and its content. Note, this function is called
  263.     via the macro ForgetCollection(). Macro defined in the header.
  264. ******************************************************************************/
  265.  
  266. pascal void __ForgetCluster( Ptr *aClusterAddr )
  267. {
  268.     CCluster *theCluster = (CCluster*) *aClusterAddr;
  269.     
  270.     if ( theCluster != NULL )
  271.     {
  272.         *aClusterAddr = NULL;
  273.         theCluster->DisposeAll();
  274.     }
  275.  
  276. } /* __ForgetCluster */
  277.  
  278.